package hotel.base.oversea.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lmf
 * @Date 2020/8/7 13:43
 * 把list 按照拆分容量分为几等分
 **/
@Slf4j
public class ListUtils {
    public static <T> List<List<T>> parititon(final List<T> list,final int size){
        if(ObjectUtils.isEmpty(list)){
            log.info("===========待拆分的list为空===========");
           return new ArrayList<>();
        }
        if(size<=0){
            log.info("===========容量size小于1===========");
           return new ArrayList<>();
        }
        return new Partition<>(list,size);
    }
    private static class  Partition<T> extends AbstractList<List<T>>{
        private final  List<T> list;
        private final int size;

        private Partition(List<T> list,final int size) {
            this.list = list;
            this.size=size;
        }

        @Override
        public List<T> get(int index) {
            final int listSize=size();
            if(index<0){
                log.info("========起始位置index小于0===============");
                return new ArrayList<>();
            }
            if(index>=listSize){
                log.info("========起始位置index小于最大长度===============");
                return new ArrayList<>();
            }
            final int start=index*size;
            final int end=Math.min(start+size,list.size());
            return list.subList(start,end);
        }

        @Override
        public int size() {
            return (int) Math.ceil((double) list.size() / (double) size);
        }
    }

    public static void main(String[] arg){
        List<String> list=new ArrayList<>();
        for(int i=0;i<700;i++){
            list.add("a"+i);
        }
        List<List<String>> sublist=ListUtils.parititon(list,0);
        int page=0;
        for(List<String> list1:sublist){
            for(String s:list1){
                System.out.println("page为"+page+"args"+s);
            }
            page++;
        }
        System.out.println(sublist.size());
    }
}
